package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.util.SecurityUtil;
import com.huiminpay.merchant.service.MercahntService;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.vo.MerchantDetailVO;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import com.yh.merchant.api.MerchantServiceApi;
import com.yh.merchant.dto.MerchantDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: HWW
 * @Date: 2021/8/1715:46
 * @Desc:
 */
@RestController
@Slf4j
public class MerchantController {

    @Reference
    private MerchantServiceApi merchantServiceApi;

    @Autowired
    private MercahntService mercahntService;

    @Autowired
    private SmsService smsService;

    @GetMapping("find/{id}")
    public MerchantDTO fiandMerchantById(@PathVariable Long id){
        return merchantServiceApi.queryMerchantById(id);
    }

    //向手机发送短信
    @ApiOperation("发送验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType ="String", paramType = "query")
    @GetMapping("/sms")
    public String getSMSCode(@RequestParam String phone) {
        log.info("向手机号:{}发送验证码", phone);

        return smsService.sendSms(phone);
    }

    @ApiOperation("商户注册")
    @ApiImplicitParam(name = "merchantRegisterVo", value = "注册信息", required = true, dataType ="MerchantRegisterVo", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo merchantRegisterVo){
        return mercahntService.registerMerchant(merchantRegisterVo);
    }

    @ApiOperation("资质申请-上传证件照")
    @PostMapping("/upload")
    public String upload(@ApiParam(value = "证件照",required = true) @RequestParam("file") MultipartFile multipartFile){

        return mercahntService.upload(multipartFile);
    }

    @ApiOperation("资质申请")
    @PostMapping("/my/merchants/save")
    public MerchantDTO applyMerchant(@RequestBody MerchantDTO merchantDTO){
        //解析token,取出当前登录的商户ID
        Long merchantId = SecurityUtil.getMerchantId();
        return merchantServiceApi.applyMerchant(merchantId,merchantDTO);
    }

}
